﻿using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CuaHangLego.Bussiness
{
    public class ShoppingCartBSS
    {
        public int CalculateTotal(IList<CTHD> cthds)
        {
            var data = from n in cthds
                       select n.SoLuong * n.SanPham.GiaBan;
            int total = data.Sum();
            return total;
        }

        public bool CheckOut(KhachHang kh, IList<CTHD> cthds)
        {
            if (cthds.Count > 0)
            {
                int t = cthds.Sum(ct => ct.SoLuong);
                if (t == 0) return false;
            }
            if (cthds.Count > 0)
            {
                HoaDonBan hdb = new HoaDonBan();
                hdb.ThoiGian = DateTime.Now;
                hdb.KhachHang = kh;
                hdb.DsCTHD = cthds;
                return HoaDonBanDA.Instance.Insert(hdb) != -1;
            }
            return false;
        }

        public bool CheckQuantity(CTHD ct)
        {
            return (SanPhamDA.Instance.GetQuantity(ct.SanPham.MaSo) - ct.SoLuong) >= 0;
        }
    }
}
